<?php
/////////////////////////////////////////////////////
/// 函数名称：gen 
/// 函数作用：产生静态的html页面
/// 函数作者: DH
/// 作者地址: http://linkyou.org/ 
/////////////////////////////////////////////////////

header('Content-Type:text/html;charset= UTF-8'); 

#需要使用的基础函数
include("../config.php");

$DH_output_path= $_SERVER['DOCUMENT_ROOT'] . '/dhrss/';
$DH_src_path= $_SERVER['DOCUMENT_ROOT']. '/dhrss/gen/';
$DH_html_path= $DH_src_path . 'html/';
$DH_archivers_url= 'http://127.0.0.1/dhrss';

//页面显示的文章列表条目的个数
$DH_page_count_limit = 4;
//分页中能展示的页面数
$DH_page_range = 6;


$path_foot_html  = $DH_html_path . 'foot.html';
$DH_foot = dh_file_get_contents($path_foot_html);
$path_head_html  = $DH_html_path . 'head.html';
$DH_head = dh_file_get_contents($path_head_html);
$DH_head = str_replace("%home%",$DH_archivers_url,$DH_head);
$path_meta_html  = $DH_html_path . 'meta.html';
$DH_meta = dh_file_get_contents($path_meta_html);	


$conn=mysql_connect ($dbip, $dbuser, $dbpasswd) or die('数据库服务器连接失败：'.mysql_error());
mysql_select_db($dbname, $conn) or die('选择数据库失败');
mysql_query("set names utf8;");
dh_gen_static();


function dh_gen_static()
{
	dh_gen_index();
	dh_gen_catlist();
	dh_gen_newlist();
}

function dh_gen_newlist()
{
	global $DH_html_path,$DH_output_path,$DH_foot,$DH_head,$DH_meta,$conn,$pagecount;

	$path_catlist_html  = $DH_html_path . 'catlist.html';
	$DH_catlist = dh_file_get_contents("$path_catlist_html");
	
	$DH_side = dh_get_side();
	$DH_catlist = str_replace("%side%",$DH_side,$DH_catlist);
	
	$DH_catlist = str_replace("%foot%",$DH_foot,$DH_catlist);
	$DH_catlist = str_replace("%head%",$DH_head,$DH_catlist);
	$DH_catlist = str_replace("%meta%",$DH_meta,$DH_catlist);


	$path_list_side_html  = $DH_html_path . 'list_side.html';
	$DH_list_side = dh_file_get_contents($path_list_side_html);

	$sql="select count(*) from links order by pubdate asc";
	$results=mysql_query($sql,$conn);
	$count = mysql_fetch_array($results);
	echo $count[0];
	echo '</br>';
	$end=ceil($count[0]/$pagecount);
	
	$sql="select * from links order by pubdate desc";	
	dh_gen_each_file($sql,$DH_catlist,$DH_list_side,$end,'',$end,'new');	
	
	//生产变动的页面
	$path_page_php  = $DH_html_path . 'page.php';
	$DH_page_php = dh_file_get_contents($path_page_php);
	$DH_page_php = str_replace("%max_page%",$end,$DH_page_php);
	$file_page_php = $DH_output_path . 'page.php';
	dh_file_put_contents($file_page_php,$DH_page_php);
	
}

function dh_gen_catlist()
{
	global $DH_html_path,$DH_foot,$DH_head,$DH_meta,$conn;

	$path_catlist_html  = $DH_html_path . 'catlist.html';
	$DH_catlist = dh_file_get_contents("$path_catlist_html");
	
	$DH_side = dh_get_side();
	$DH_catlist = str_replace("%side%",$DH_side,$DH_catlist);
	
	$DH_catlist = str_replace("%foot%",$DH_foot,$DH_catlist);
	$DH_catlist = str_replace("%head%",$DH_head,$DH_catlist);
	$DH_catlist = str_replace("%meta%",$DH_meta,$DH_catlist);

	
	$path_list_side_html  = $DH_html_path . 'list_side.html';
	$DH_list_side = dh_file_get_contents($path_list_side_html);
	
	$sql="select * from cat;";
	$results=mysql_query($sql,$conn);
	$catresults=array();
	if($results)
	{		
		while($row = mysql_fetch_array($results))
		{
			dh_gen_each_catlist($DH_catlist,$DH_list_side,$row['id'],$row['catname'],$row['genpage'],$row['maxid']);
		}
	}	
}

function dh_gen_index()
{
	global $DH_html_path,$DH_output_path,$DH_foot,$DH_head,$DH_meta;

	$path_index_html  = $DH_html_path . 'index.html';
	$DH_index = dh_file_get_contents("$path_index_html");
	$DH_list_cats = dh_get_cats();
	$DH_index = str_replace("%list_cat%",$DH_list_cats,$DH_index);
	$DH_list_top = dh_get_top();
	$DH_index = str_replace("%list_top%",$DH_list_top,$DH_index);
	
	$DH_index = str_replace("%foot%",$DH_foot,$DH_index);
	$DH_index = str_replace("%head%",$DH_head,$DH_index);
	$DH_index = str_replace("%meta%",$DH_meta,$DH_index);
	
	$file_index_html = $DH_output_path . 'index.html';
	dh_file_put_contents($file_index_html,$DH_index); 	
}

function dh_gen_each_catlist($DH_catlist,$DH_list_side,$id,$catname,$genpage,$maxid)
{
	global $DH_html_path,$DH_output_path,$conn,$pagecount,$pagebgen;
	$sql="select count(*) from links where catid = ".$id;
	$results=mysql_query($sql,$conn);
	$catcount = mysql_fetch_array($results);
	echo $catcount[0];
	echo '</br>';
	//a是正常的，b是为了满足观看的需要，需要将页面提前几页满页显示
	$abegin=$genpage+1;
	echo $abegin;
		echo '</br>';
	$bend=ceil($catcount[0]/$pagecount);
	echo $bend;
		echo '</br>';
	$bbegin=$bend-$pagebgen+1;
	echo $bbegin;
		echo '</br>';	
	//生产变动的页面

	$path_page_php  = $DH_html_path . 'page.php';
	$DH_page_php = dh_file_get_contents($path_page_php);
	$DH_page_php = str_replace("%max_page%",$bend,$DH_page_php);
	$file_page_php = $DH_output_path . 'page'.$id.'.php';
	dh_file_put_contents($file_page_php,$DH_page_php);
	
	//输出最前面的页面
	$sql="select * from links where catid = ".$id ." order by id desc limit 0,".$pagebgen*$pagecount;
	echo $sql;
	echo '</br>';
	dh_gen_each_file($sql,$DH_catlist,$DH_list_side,$bend,$id,$bend,'cat');
	
	//输出需要增加的中间的页面
	$abeginid=$maxid+1;
	//$aendid=$middleend-1;
	$apages=($bbegin-$abegin)*$pagecount-1;
	$sql="select id from links where catid = ".$id." and id >= ".$abeginid ." order by id asc limit ".$apages.",1";
	echo $sql;
	echo '</br>';
	$results=mysql_query($sql,$conn);
	$aendid = mysql_fetch_array($results);
	print_r($aendid);
	echo $aendid[0];
	echo '</br>';
	//真正的起点和终点
	$sql="select * from links where catid = ".$id." and id between ".$abeginid ." and ".$aendid[0]." order by id desc";
	echo $sql;
	echo '</br>';
	dh_gen_each_file($sql,$DH_catlist,$DH_list_side,$bbegin-1,$id,$bend,'cat');
	
}

function dh_gen_each_file($sql,$DH_catlist,$DH_list_side,$lastpage,$id,$maxpage,$filename)
{
	global $DH_archivers_url,$DH_output_path,$conn,$pagecount;
	$count=0;
	$results=mysql_query($sql,$conn);	
	if($results)
	{	
		$liout='';
		while($row = mysql_fetch_array($results))
		{
			$count++;
			echo $row['id'];
			echo $row['title'];
			echo '</br>';
			
			//$numid = $lastpage*$pagecount+1- $count;
			$numid = ($count-1)%$pagecount+1;
			$update=$row['pubdate'];
			$author=$row['author'];
			$linkid=$row['id'];
			$link=$row['link'];
			$title=$row['title'];
			$updatef = date("m-d H:i",strtotime($update));
			//$updatef2='07/05';				
			$liout.='<li><span>'.$updatef.'</span><a href="'.$link.'" target="_blank"> <?php echo ($paged-1)*$pagecount+'.$numid.';?> ['.$author.'] '.$linkid.' '.$title.'</a> </li>';			
			if($count%$pagecount==0)
			{
				$catpage =$maxpage+1-($lastpage+1-($count/$pagecount));
				echo 'genpage:'.$catpage.'</br>';
				$DH_list_side_new = str_replace("%max_page%",$maxpage,$DH_list_side);
				$link=$DH_archivers_url.'/'.$filename.$id.'-';
				$DH_list_side_new = str_replace("%link%",$link,$DH_list_side_new);
				$DH_list_side_new = str_replace("%paged%",$catpage,$DH_list_side_new);
				$DH_list_side_new = str_replace("%pagecount%",$pagecount,$DH_list_side_new);
				$DH_list_side_new = str_replace("%catid%",$id,$DH_list_side_new);
				$DH_list_side_new = str_replace("%list%",$liout,$DH_list_side_new);	
				$DH_catlist_new= str_replace("%list_side%",$DH_list_side_new,$DH_catlist);	
				$file_catlist_html = $DH_output_path . $filename.$id.'-'.$catpage.'.html';
				dh_file_put_contents($file_catlist_html,$DH_catlist_new);
				$liout='';
			}
		}
	}	
}

function dh_get_cats()
{
	global $DH_archivers_url,$DH_html_path,$conn;	
	
	$sql="select * from cat;";
	$results=mysql_query($sql,$conn);
	$catresults=array();
	if($results)
	{		
		while($row = mysql_fetch_array($results))
		{
			$cateach = array();
			array_push($cateach,$row['id']);
			array_push($cateach,$row['catname']);
			array_push($catresults,$cateach);		
		}
	}
	//print_r($catresults);
	
	$DH_list_cats='';
	foreach ($catresults as $cat)
	{
		$catid = $cat[0];
		$catname = $cat[1];
		$sql="select * from links where catid = $catid limit 0,10;";
		$recs=mysql_query($sql,$conn);
		$DH_list_cat = '';
		if($recs)
		{
			$path_list_cat_html  = $DH_html_path . 'list_cat.html';
			$DH_list_cat = dh_file_get_contents($path_list_cat_html);
			$liout='';
			while($row = mysql_fetch_array($recs))
			{
				$update=$row['pubdate'];
				$link=$row['link'];
				$title=$row['title'];
				$author=$row['author'];
				$updatef = date("Y-m-d H:i:s",strtotime($update));
				$updatef2='07/05';				
				$liout.='<li><span>'.$updatef2.'</span><a href="'.$link.'" target="_blank">['. $author .'] '.$title.'</a> </li>';		
			}
			$DH_list_cat = str_replace("%list_new%",$liout,$DH_list_cat);
			$list_new_more = $DH_archivers_url . '/cat'.$catid.'-1.html';
			$DH_list_cat = str_replace("%list_new_more%",$list_new_more,$DH_list_cat);
			$DH_list_cat = str_replace("%list_cat%",$catname,$DH_list_cat);
		}
		$DH_list_cats .=$DH_list_cat;
	}
	return $DH_list_cats;
}

function dh_get_top()
{
	global $DH_html_path,$conn,$DH_archivers_url;	
	$sql="select * from links limit 0,10;";
	$recs=mysql_query($sql,$conn);
	$DH_list_top = '';
	if($recs)
	{
		$path_list_top_html  = $DH_html_path . 'list_top.html';
		$DH_list_top = dh_file_get_contents($path_list_top_html);
		$liout='';
		while($row = mysql_fetch_array($recs))
		{
			$update=$row['pubdate'];
			$link=$row['link'];
			$title=$row['title'];
			$catname=$row['catname'];
			$updatef = date("Y-m-d H:i:s",strtotime($update));
			$updatef2='07/05';				
			$liout.='<li><span>'.$updatef2.'</span><a href="'.$link.'" target="_blank">['.$catname.'] '.$title.'</a> </li>';		
		}
		$new_more = $DH_archivers_url . '/new-1.html';
		$DH_list_top = str_replace("%new_more%",$new_more,$DH_list_top);
		$DH_list_top = str_replace("%list_top%",$liout,$DH_list_top);
	}
	return $DH_list_top;
}

function dh_get_list_side()
{
	global $DH_html_path,$conn;	
	$path_list_side_html  = $DH_html_path . 'list_side.html';
	$DH_list_side = dh_file_get_contents($path_list_side_html);

	return $DH_list_side;
}

function dh_get_side()
{
	global $DH_html_path,$conn;	
	$path_side_html  = $DH_html_path . 'side.html';
	$DH_side = dh_file_get_contents($path_side_html);
	return $DH_side;
}

function dh_file_get_contents($filename) {
	$fh = fopen($filename, 'rb', false);

	clearstatcache();
	if ($fsize = @filesize($filename)) {
		$data = fread($fh, $fsize);
	} else {
		$data = '';
		while (!feof($fh)) {
			$data .= fread($fh, 8192);
		}
	}
	fclose($fh);
	return $data;
}

function dh_file_put_contents($filename, $content) {
	
	// Open the file for writing
	$fh = @fopen($filename, 'wb', false);
	// Write to the file
	@fwrite($fh, $content);
	// Close the handle
	@fclose($fh);
}
?>